package ca.usherbrooke.view.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.Properties;

/**
 * 
 * @author Ahmed
 * 
 */

public class PropertieBase implements Serializable {
	private static final long serialVersionUID = 1L;

	private Properties properties;
	private File currentPath;
	private final String propertiesFile;

	public PropertieBase(String fileName) throws FileNotFoundException,
			IOException {
		propertiesFile = fileName;
		this.properties = load(fileName);
	}

	private Properties load(String file) throws IOException,
			FileNotFoundException {
		currentPath = new File(file).getParentFile();
		Properties properties = new Properties();
		FileInputStream input = new FileInputStream(file);
		try {
			properties.load(input);
			return properties;

		} finally {
			input.close();
		}
	}

	public String getFilePath() {
		return this.propertiesFile;
	}

	public Integer getIntegerValue(String key, Integer defaultVal) {
		return Integer.valueOf(properties.getProperty(key,
				defaultVal.toString()));
	}

	public String getValue(String key) {
		return properties.getProperty(key);
	}

	public String getLocalPath(String localPath) throws IOException {
		File f = new File(currentPath, localPath);
		localPath = f.getCanonicalPath();
		return localPath;
	}

	public String getPath(String key) throws IOException {
		String path = properties.getProperty(key);
		File f = new File(currentPath, path);
		path = f.getCanonicalPath();
		return path;
	}

	public Boolean getBooleanValue(String key) {
		return Boolean.valueOf(properties.getProperty(key));
	}

	public Integer getIntegerValue(String key) {
		return Integer.valueOf(properties.getProperty(key));
	}

	public Float getFloatValue(String key) {
		return Float.valueOf(properties.getProperty(key));
	}

	public Properties getName() {
		return properties;
	}

	public void setName(Properties name) {
		this.properties = name;
	}

}
